package com.rmdjzz.slmmn.controller.user.userMessageManagement;

/*@创建时间   2023/6/8
 * @作者 lzy
 * @内容 用户登录后的页面信息
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.rmdjzz.slmmn.pojo.base.User;
import com.rmdjzz.slmmn.pojo.result.Result;
import com.rmdjzz.slmmn.pojo.vo.UserMessagesVo;
import com.rmdjzz.slmmn.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@Api(tags = "用户登录后的个人信息")
@RequestMapping("/user")
@Slf4j
public class userMessageManagenent {
    @Autowired
    private UserService userService;

    @GetMapping("/userMessages")
    @ApiOperation("个人信息首页")
    public String getUserLoginIndex() {
        return "test";
    }

    //通过id修改密码，显示账号
    @GetMapping("/userSafety")
    @ApiOperation("用户账号安全/修改密码")
    @ResponseBody
    public Result userSafety(UserMessagesVo userMessagesVo){
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", userMessagesVo.getId()).set("password", userMessagesVo.getPassword());
        userService.update(updateWrapper);
        return Result.ok();
    }

    @GetMapping("/userChangeMessage")
    @ApiOperation("用户账号修改信息")
    @ResponseBody
    public  Result  userChangeMessage(User users, HttpSession session){
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("user_name", users.getUserName())
                .set("age",users.getAge())
                .set("sex",users.getSex())
                .eq("id",users.getId());
        //更新用户
        userService.update(updateWrapper);
        //条件构造器-查询用户
        //session未更新
        User user =(User) session.getAttribute("user");
        Integer id = user.getId();
        //查询更新后的用户
        User byId = userService.getById(id);
        //更新session
        session.setAttribute("user",byId);
        return  Result.ok();
    }

    @GetMapping("/userGetAll")
    @ApiOperation("得到用户")
    @ResponseBody
    public Result userGetAll(HttpSession session){
        User user =(User) session.getAttribute("user");
        return Result.ok(user);
    }

}
